
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 2711
 * @Author : WangRuoyu 
 * @Date : 2023/5/29 10:54
 */

#include "iostream"
#include "vector"
#include "unordered_set"

using namespace std;

class Solution {
public:
    vector<vector<int>> differenceOfDistinctValues(vector<vector<int>> &grid) {
        int m = grid.size();
        int n = grid[0].size();
        vector<vector<int>> res(grid.size(), vector<int>(grid[0].size(), 0));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                unordered_set<int> set;
                int x = i - 1, y = j - 1;
                while (x >= 0 && x < m && y >= 0 && y < n) {
                    set.insert(grid[x][y]);
                    x--;
                    y--;
                }
                int tmp = set.size();
                set.clear();
                x = i + 1, y = j + 1;
                while (x >= 0 && x < m && y >= 0 && y < n) {
                    set.insert(grid[x][y]);
                    x++;
                    y++;
                }
                tmp -= set.size();
                res[i][j] = abs(tmp);
            }
        }
        return res;
    }
};

int main() {
    Solution sl;
    vector<vector<int>> grid = {{1, 2, 3},
                                {3, 1, 5},
                                {3, 2, 1}};
    for (vector<int> i: sl.differenceOfDistinctValues(grid)) {
        for (int j: i) {
            cout << j << " ";
        }
        cout << endl;
    }
    return 0;
}